import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {HttpService} from "../../providers/HttpService";
import {InfoProvider} from "../../providers/InfoProvider";

/**
 * Generated class for the DiscoveryPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-discovery',
  templateUrl: 'discovery.html',
})
export class DiscoveryPage {

    user_info: any;
    share_list: any[] =[];

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private infoProvider: InfoProvider,
                private httpService: HttpService) {
        this.user_info = this.infoProvider.getUserInfo();
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad DiscoveryPage');
    }

    ionViewWillEnter() {
        this.getDailyShare().then(res => {
            if (res.status == 0) {
                this.share_list = res.data.rows;
            }
        });
    }

    getDailyShare(loader: boolean = true): Promise<any> {
        return new Promise(resolve => {
            this.httpService.instance().setUrl('/dailyShare/getDailyShare')
                .setQuery({
                    status: 1,
                    start: 0,
                    size: 10,
                    orderKey: 'id',
                    orderVal: 'desc'
                })
                .showLoader(loader)
                .getRequest()
                .then(res => {
                    resolve(res);
                });
        });
    }

    onImageTap() {

    }

    onRefresh(refresher) {
        this.getDailyShare(false).then(res => {
            if (res.status == 0) {
                this.share_list = res.data.rows;
            }
            refresher.complete();
        });
    }

    onPubDiscovery() {
        this.navCtrl.push('PubDiscoveryPage');
    }
}
